package RMI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.BoxLayout;

public class ClientFrame extends JFrame implements MessageEventListeners{

	private JPanel contentPane;
	private JPanel panelTop;
	private JPanel panel_Button;
	private JButton btnSend;
	private JTextField textChatSendField;
	private btnController btnController = new btnController();
	private JPanel panel;
	private JTextField textField;
	private static String host = null;
	private static Chat stub;
	private static Registry registry;
	private JButton btnGetMessage;
	private String name = "chat";
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					registry = LocateRegistry.getRegistry(host);
					stub = (Chat) registry.lookup("chat");
					
					System.out.println(stub.toString());
					
					ClientFrame frame1 = new ClientFrame();
//					ClientFrame frame2 = new ClientFrame();
					
					stub.addMessageEventListeners(frame1);
//					stub.addMessageEventListeners(frame2);
					
					
					System.out.println("Client Online");
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientFrame() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		panelTop = new JPanel();
		contentPane.add(panelTop, BorderLayout.NORTH);
		
		btnGetMessage = new JButton("GetMessage");
		panelTop.add(btnGetMessage);
		btnGetMessage.addActionListener(btnController);
		
		panel_Button = new JPanel();
		contentPane.add(panel_Button, BorderLayout.SOUTH);
		panel_Button.setLayout(new BoxLayout(panel_Button, BoxLayout.X_AXIS));
		
		textChatSendField = new JTextField();
		panel_Button.add(textChatSendField);
		textChatSendField.setColumns(10);
		
		btnSend = new JButton("Send");
		panel_Button.add(btnSend);
		btnSend.addActionListener(btnController);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		textField = new JTextField();
		panel.add(textField, BorderLayout.CENTER);
		textField.setColumns(10);
		
		
		
		setVisible(true);
	}
	
	private class btnController implements ActionListener, Serializable{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == btnSend){
				//TODO
				try {
					stub.sendMessage(textChatSendField.getText());
					clearField();
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
				System.out.println("Message send");
			}
			if(e.getSource() == btnGetMessage){
				try {
					
					textChatSendField.setText(stub.reviveMessage());
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				}
			
			
		}
		
	}
	
	public void clearField(){
		textChatSendField.setText("");
	}
	
	@Override
	public void messageEvent(MessageEvent evt) {
		
			Chat stub = (Chat) evt.getSource();
			System.out.println(stub.toString());
					
	}

}
